package com.chf.control;

import com.chf.model.UserModel;
import com.chf.utils.MyUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

//注释 用于设置servlet的名字与路径射映
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //重定向
//        response.sendRedirect("WEB-INF/test01.jsp");  //无法访问，会报错，必需通过服务器转发才行
//        response.sendRedirect("main.jsp");
        //转发  可以访问安全目录下的文件
//        request.getRequestDispatcher("WEB-INF/test01.jsp").forward(request,response);
        //获取web端的表单数据
        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");
        System.out.println(userName);
        System.out.println(userPwd);
        request.setAttribute("userName",userName);
//        UserModel userModel = new UserModel();
        boolean canLogin = MyUtils.GetNewInstance(UserModel.class).CanLogin(userName,userPwd);
        if(canLogin){
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }else {
            response.sendRedirect("index.jsp");
        }
    }
}
